#include <iostream>
#include <algorithm>
#include<cmath>
#include<string>
#include<stack>
using namespace std;

bool chkParenthesis(string A, int n)
{
    // write code here
    stack<char> sc;
    int i = 0;
    do
    {
        if (A[i] != ')')
            sc.push(A[i]);
        else if (A[i] == ')')
        {
            while (sc.top() != '(')
            {
                sc.pop();
            }
            sc.pop();
        }
        i++;
    } while (i < n && !sc.empty());

    if (sc.empty())
        return true;
    else
        return false;

}
void main()
{
    string s("(a)(aa)()");
    cout << chkParenthesis(s, s.size());

}